package com.echo.sdk.im.handler;


import com.echo.sdk.im.constant.CIMConstant;
import com.echo.sdk.im.model.CIMSession;
import com.echo.sdk.im.model.CIMMsg;
import com.echo.sdk.im.service.CIMSessionService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;


/**
 * @author chentl
 * @version V1.0
 * @Project IM-SDK
 * @Title SessionClosedHandler.java
 * @Description 断开连接，清除session
 * @date 2020/9/6 3:59 下午
 */
@Component
public class SessionClosedHandler implements CIMRequestHandler {

    @Resource
    private CIMSessionService cimSessionService;


    @Override
    public void process(CIMSession session, CIMMsg cimMsg) {
        Object quietly = session.getAttribute(CIMConstant.KEY_QUIETLY_CLOSE);
        if (Objects.equals(quietly, true)) {
            return;
        }

        Object account = session.getAttribute(CIMConstant.KEY_ACCOUNT);
        if (account == null) {
            return;
        }

        cimSessionService.remove(session.getNid());
    }

}
